package com.xuetang9.fujian.studentdemo.util;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

/**
 * @author: fujian
 * @description: 查询数据库的工具类，封装了一些常用的查询操作
 * @date: 2021/5/18 15:10
 * @version: V1.0
 * @package: com.xuetang9.fujian.studentdemo.util
 **/
@SuppressWarnings("all")
public abstract class BaseDao {
    public static Connection connection = null;
    private static Properties properties = null;
    public Statement statement = null;
    public PreparedStatement preparedStatement = null;
    public ResultSet resultSet = null;
    // 使用静态块加载配置文件中的资源，加载mysql的驱动
    static {
        properties = new Properties();
        try {
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbcConfig/jdbcConfig.properties"));
            Class.forName(properties.getProperty("jdbc.driver"));
//            properties.getProperty("jdbc.driver");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /*
     * @Description: 获取数据库的连接对象
     * @param:
     * @return java.sql.Connection
     * @author: fujian
     * @date 2021/5/18 15:24
    */
    public static Connection GetConnection(){
        String url = properties.getProperty("jdbc.url");
        String userName = properties.getProperty("jdbc.userName");
        String password = properties.getProperty("jdbc.password");
        try {
            connection = DriverManager.getConnection(url,userName,password);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
    
    /*
     * @Description: 关闭资源
     * @param: resultSet
     * @param: statement
     * @param: preparedStatement
     * @param: connection
     * @return void
     * @author: fujian
     * @date 2021/5/18 15:27
    */
    public void close(ResultSet resultSet, Statement statement,PreparedStatement preparedStatement,Connection connection){
        try{
            free(resultSet);
            free(statement);
            free(preparedStatement);
            free(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /*
     * @Description: 公共的关闭资源方法
     * @param: closeable 可以关闭接口
     * @return void
     * @author: fujian
     * @date 2021/5/18 15:28
    */
    public void free(AutoCloseable closeable){
        if (Objects.isNull(closeable))return;
        try {
            closeable.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
     * @Description:使用模板模式封装的查询方法，可以进行全部数据的查询，单个数据的查询
     * @param: sql
     * @param: params
     * @return java.util.List<java.lang.Object>
     * @author: fujian
     * @date 2021/5/18 15:29
    */
    public List<Object> executeQuery(String sql, Object ... params){
        List<Object> objList = new ArrayList<>();
        connection = GetConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            if (Objects.nonNull(params)){
                for (int i = 0; i < params.length; i++) {
                    preparedStatement.setObject(i+1,params[i]);
                }
            }
            resultSet = preparedStatement.executeQuery();
            fillList(objList,resultSet);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(resultSet,statement,preparedStatement,connection);
        }
        return  objList;
    }
    /*
     * @Description: 字类进行实现数据的填充
     * @param: objList
     * @param: resultSet
     * @return void
     * @author: fujian
     * @date 2021/5/19 0:22
    */
    public abstract void fillList(List<Object> objList, ResultSet resultSet) throws SQLException;

}
